#!/usr/bin/env ruby

require 'Xml'
require 'Activity'
require 'DataObject'


class Spring < Xml
  ACTIVITY = { :bean => "bean", :service => "service", :reference => "reference" }
  
  
  def activity_collect(node)
    act = Activity.new
    act.name = node["name"].nil? ? node["id"] : node["name"]
    act.type = node.name
    begin
      if act.type == ACTIVITY[:bean]
	act.reference = node.xpath("#{node.xpath}//@ref")[0].value
      else
	act.reference = node["target"]
      end
    rescue
    end
      act.operation
    begin
      act.input = [variable_to_data(node)]
    rescue
    end
    begin
      act.output = [variable_to_data(node.xpath("/*/*[(@name|@id) = '#{node.xpath("#{node.path}//@*[contains('ref target',local-name())]")[0].value}']")[0])]
    rescue
    end
    act.description
    act
  end

  def activities
    activities_doc.collect do |el|
      activity_collect el
    end
  end

  def activities_doc
    doc.xpath('//*').collect do |el|
      if ACTIVITY.has_value? el.name
	el
      end
    end.compact
  end

  def variable_to_data(node)
      var = DataObject.new
      cls = (node.name == ACTIVITY[:bean]) ? node["class"] : node["type"]
      var.type = 
      var.name = cls.split(".")[-1]
      var.namespace = cls.split(".")[0...-1].join(".")
      var.resource_type = "class"
      var.description
      var
  end

  def name
    path.split("/")[-1].split(".")[0]
  end

  def type
    SPRING
  end

end

